﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FlashCards.WebApp.Utils;
using FlashCards.WebApp.Models;
using FlashCards.WebApp.DataAccessLayer;

namespace FlashCards.WebApp.Controllers
{
    [Authorize]
    public class GroupsController : Controller
    {
        //
        // GET: /Groups/

        public ActionResult Index()
        {
            if (!string.IsNullOrEmpty(User.Identity.Name))
            {
                var groups = GroupUtils.GetGroupsWhereMember(User.Identity.Name);

                var model = groups.Select(x => new GroupModel(
                    x,
                    x.GroupMembers.Select(y => y.User).ToList(),
                    x.FlashcardSets.ToList()))
                    .ToList();

                return View(model);
            }

            return View();
        }

        public ActionResult CreateGroup()
        {
            return View(new AddGroupModel());
        }

        [HttpPost]
        public ActionResult CreateGroup(AddGroupModel model)
        {
            if (ModelState.IsValid)
            {
                GroupUtils.AddNewGroup(model.Name, model.IsHidden, User.Identity.Name);
                return RedirectToAction("Index", "Groups");
            }
            return View(model);
        }

        public ActionResult ViewGroupMembers(int? groupId)
        {
            if (groupId.HasValue)
            {
                var users = GroupUtils.GetGroupMembers(groupId.Value);
                var groupOwner = GroupUtils.GetGroupOwnerNick(groupId.Value);
                var group = GroupUtils.GetGroupById(groupId.Value);
                var model = new GroupMembersModel(group, users, groupOwner);

                return View(model);
            }

            ModelState.AddModelError("", "Group name is empty");
            return View("Error");
        }

        public ActionResult AddFriendToGroup(int? groupId)
        {
            if (groupId.HasValue)
            {
                var friends = FriendshipUtils.GetUserFriends(User.Identity.Name);
                var groupMembers = GroupUtils.GetGroupMembers(groupId.Value);
                var group = GroupUtils.GetGroupById(groupId.Value);
                var model = new AddFriendToGroupModel(group, friends, groupMembers);

                return View(model);
            }

            return View("Error");
        }

        [HttpPost]
        public ActionResult AddFriendToGroup(int? groupId, int? friendId)
        {
            if (groupId.HasValue && friendId.HasValue)
            {
                GroupUtils.AddUserToGroup(groupId.Value, friendId.Value);

                return RedirectToAction("ViewGroupMembers", new { groupId = groupId });
            }

            return View("Error");
        }

        public ActionResult ViewGroupFlashcardSets(int? groupId)
        {
            if (groupId.HasValue)
            {
                var userId = UserUtils.GetUserId(User.Identity.Name);
                var flashcardSets = GroupUtils.GetGroupFlashcardSets(groupId.Value);
                var group = GroupUtils.GetGroupById(groupId.Value);

                if (userId.HasValue)
                {
                    var privilege = GroupUtils.GetUserGroupPrivilege(userId.Value, groupId.Value);
                    var model = new GroupFlashcardsModel(group, flashcardSets, privilege, userId.Value);
                    return View(model);
                }
            }
            return View("Error");
        }

        public ActionResult SetUserGroupRole(int? userId, int? groupId)
        {
            if (userId.HasValue && groupId.HasValue)
            {
                var userRole = GroupUtils.GetUserGroupPrivilege(userId.Value, groupId.Value);
                var roles = GroupUtils.GetSettableGroupRoles();
                var group = GroupUtils.GetGroupById(groupId.Value);
                var user = UserUtils.GetUser(userId.Value);

                var model = new SetUserGroupRoleModel(user, group, userRole, roles);
                return View(model);
            }

            return View("Error");
        }

        [HttpPost]
        public ActionResult SetUserGroupRole(FormCollection form, int groupId, int userId)
        {
            var selectedRole = form["UserRole"];

            GroupUtils.SetUserGroupRole(userId, groupId, selectedRole);

            return RedirectToAction("ViewGroupMembers", new { groupId = groupId });
        }

        public ActionResult DeleteUserFromGroup(int? userId, int? groupId)
        {
            if (userId.HasValue && groupId.HasValue)
            {
                ViewBag.UserId = userId;
                ViewBag.GroupId = groupId;
                return View();
            }

            return View("Error");
        }

        [HttpPost]
        public ActionResult DeleteUserFromGroup(int? userId, int? groupId, int fakeParam)
        {
            if (groupId.HasValue && userId.HasValue)
            {
                GroupUtils.RemoveUserFromGroup(userId.Value, groupId.Value);
                return RedirectToAction("ViewGroupMembers", new { groupId = groupId });
            }

            return View("Error");
        }

        public ActionResult AddExistingFlashcardSetToGroup(int? groupId)
        {
            if (groupId.HasValue)
            {
                var sets = FlashcardUtils.GetUserFlashcardSets(User.Identity.Name, false, true);
                var userId = UserUtils.GetUserId(User.Identity.Name);
                var group = GroupUtils.GetGroupById(groupId.Value);
                var model = new AddExistingFlashcardSetToGroupModel(userId.Value, sets, group);
                return View(model);
            }
            return View("Error");
        }

        [HttpPost]
        public ActionResult AddExistingFlashcardSetToGroup(int? groupId, int? setId)
        {
            if (groupId != null)
            {
                if (groupId.HasValue && setId.HasValue)
                {
                    GroupUtils.AddExistingSetToGroup(groupId.Value, setId.Value);
                    return RedirectToAction("AddExistingFlashcardSetToGroup", new { groupId = groupId.Value});
                }
            }

            //skoro tu jesteśmy to coś się popsuło
            return View("Error");
        }

        [HttpPost]
        public ActionResult RemoveSetFromGroup(int? groupId, int? setId)
        {
            //usuwanie powiązania zestaw <-> grupa
            if (setId.HasValue && groupId.HasValue)
            {
                GroupUtils.RemoveSetFromGroup(setId.Value);
                return RedirectToAction("AddExistingFlashcardSetToGroup", new { groupId = groupId.Value });
            }

            return View("Error");
        }

    }
}
